#!/usr/bin/perl

my $params;
@dpl = qw(0 0.25 0.5 0.75 1);
@scl = qw(0.001 0.01 0.1 0.5 1 2);
@sml = qw(10^-5 10^-6 10^-7 10^-8);
@nml = qw(10^-4 10^-5 10^-7);
$sw = 0;

for $divprob (1..5){
    for $selcoeff (1..6){
	for $selmu (1..4){
	    for $neutmu (1..3){
		for $replicates (1..3){
		    $sw++;
		    $params[$sw][1] = $sw;
		    $params[$sw][2] = $dpl[$divprob-1];
		    $params[$sw][3] = $scl[$selcoeff-1];
		    $params[$sw][4] = $sml[$selmu-1];
		    $params[$sw][5] = $nml[$neutmu-1];
		    $params[$sw][6] = $replicates;
		}
	    }
	}
    }
}

#my @resultset = (1054,1065,1072);
my @resultset = ();

if($#resultset<1){
for $i (1..1080){
    # conditions, selective mu, sel. coeff, neutral mu
    if($params[$i][4] eq "10^-5" && $params[$i][3]==1 && $params[$i][5] eq "10^-5"){
	# if images do not exist, skip them
	if(-e "sim".$i.".grid.colored.by.selective.patterns.jpg"){
	    push @resultset, $i;
	}
    }
}
}
 

open (out,">results.tex");

print out q'\documentclass{beamer}
% Setup appearance:
\usepackage{algorithm2e}
\usepackage{verbatim}
\usetheme{Madrid}

% Standard packages
\usepackage[english]{babel}
\usepackage[latin1]{inputenc}
\usepackage{times}
\usepackage[T1]{fontenc}
\usepackage{graphicx}
\usepackage[multidot]{grffile}

% Get rid of navigation symbols
\setbeamertemplate{navigation symbols}{}

% The main document
\begin{document}

\setbeamertemplate{footline}{}';

for $i (0..$#resultset){
    print out q'
\begin{frame}{\tiny{$sim='.$params[$resultset[$i]][1].'~~r='.$params[$resultset[$i]][2].
'~~smu='.$params[$resultset[$i]][4].'~~nmu='.$params[$resultset[$i]][5].
'~~s='.$params[$resultset[$i]][3].'~~rep='.$params[$resultset[$i]][6].'$}}
  \begin{center}
  \includegraphics[height=1in]{divergence.all-'.($resultset[$i]-1).'.jpg}
  \includegraphics[height=1in]{growth-rates-of-clones-'.($resultset[$i]-1).'.jpg}
  \includegraphics[height=1in]{sim'.$resultset[$i].'.grid.colored.by.selective.patterns.jpg}  
  \includegraphics[height=1in]{sim'.$resultset[$i].'.grid.colored.by.neutral.patterns.jpg} \\\ 
  \includegraphics[height=2.2in]{sim'.$resultset[$i].'.phylogeny.colored.by.selective.patterns.jpg}
  \includegraphics[height=2.2in]{sim'.$resultset[$i].'.phylogeny.colored.by.neutral.patterns.jpg}
  \end{center}
\end{frame}
';
}

print out q'
\end{document}
';

@args = "texi2pdf -b results.tex";
system(@args);
